/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prepocessing_2.com.prep;

import java.io.File;
import java.io.FilenameFilter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

/**
 *
 * @author baga
 */
public class DocumentCollection {

    private File useDir;
    private Tokenizer tokens;
    private Dictionary dict;
    private PostingList postList;
    private Connection conn;

    public DocumentCollection(File pathDir) {
//        try {
//            String url = "jdbc:mysql://localhost:3306/xsedb";
//            Class.forName("com.mysql.jdbc.Driver");
//            conn = DriverManager.getConnection(url,
//                    "root", "");
//        } catch (ClassNotFoundException ce) {
//            ce.printStackTrace();
//        } catch (SQLException se) {
//            se.printStackTrace();
//        }
        useDir = pathDir;
        //print(useDir.getPath());
        tokens = new Tokenizer();
        dict = new Dictionary();
        postList = new PostingList();
    }

    public void exDocId() {
        if (useDir != null) {

            if (useDir.isDirectory()) {
                try {
                    File[] listFiles = local(useDir, "[aA-zZ0-9].*\\.xml");
                    //File[] listFiles = useDir.listFiles();
                    ArrayList alR = new ArrayList();
                    String url = "jdbc:mysql://localhost:3306/xsedb";
                    Class.forName("com.mysql.jdbc.Driver");
                    conn = DriverManager.getConnection(url,
                            "root", "");
                    PreparedStatement ps = conn.prepareCall("select "
                            + "doc from t_doc");
                    ResultSet rs = ps.executeQuery();
                    while (rs.next()) {
                        alR.add(rs.getString(1));
                    }
                    conn.close();
                    conn = null;
                    for (File file : listFiles) {
                        // execute All
//                        file.getName();
                        if (!alR.contains(file.getPath())) {
                            setPosting(file);
//                            instDoc(file.getPath());
                            alR.add(file.getPath());
                        }
                    }
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(DocumentCollection.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException se) {
                }
            } else {

                setPosting(useDir);
                instDoc(useDir.getPath());
            }
        }
    }

    private void instDoc(String path) {
        try {
            int tDocId = 0;
            PreparedStatement pstm = conn.prepareCall("select max(t_doc_id) "
                    + "from t_doc");
            ResultSet rs = pstm.executeQuery();
            while (rs.next()) {
                tDocId = rs.getInt(1);
            }
            pstm.clearParameters();
            pstm = conn.prepareCall("insert into t_doc "
                    + "values(?,?,current_timestamp)");
            pstm.setInt(1, ++tDocId);
            pstm.setString(2, path);
            pstm.execute();
        } catch (SQLException se) {
            se.printStackTrace();
        }
    }

    private void setPosting(File file) {
        tokens.setDocument(file);
        tokens.initialized();
        String[] tempDoc = tokens.getDocs();
        String[] tempToken = tokens.getTokens();
        System.out.println("di documentcollection doc : " + tempDoc.length + " tokens : " + tempToken.length);
        dict.setDictionary(tempDoc, tempToken);
        postList.writeBlockToDisk(tempDoc, tempToken, file.getPath(), tokens.getDocContent(), tokens.getContent());
        dict.eraseData();
    }

    private static File[] local(File dir, final String regex) {
        return dir.listFiles(new FilenameFilter() {

            private Pattern pattern = Pattern.compile(regex);

            public boolean accept(File dir, String name) {
                return pattern.matcher(new File(name).getName()).matches();
            }
        });
    }

//    public void writeBlock() {
//        postList.writeBlockToDisk(dict.getPosting(), dict.getDictionary());
//    }
    public static void main(String[] args) {
        DocumentCollection docLoc = new DocumentCollection(new File("samples"));
        docLoc.exDocId();
//        docLoc.writeBlock();
    }
}
